<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>注册</title>
    <script src="/layui/layui.all.js"></script>
    <script src="/layui/layui.js"></script>
    <link href="/layui/css/layui.css">

</head>
<body>
<h1 th:inlines="text">注册</h1>
<form  method="post" enctype="multipart/form-data">
    <input type="text" name="name" id="name" placeholder="账号"><br>
    <p>
        选择头像: <input type="file" id="fileName" name="fileName" />
    </p>
    <p>
        <input type="button" id="submit" value="提交" />
    </p>
</form>
<script src="/js/jquery-3.3.1.min.js"></script>
<script>
    layui.use(['layer', 'form'], function(){
        var layer = layui.layer;

        $("#submit").click(function () {
            var name = $("#name").val();
            var fileName = $('#fileName')[0].files[0];
            if (name == null || name == "") {
                alert("账号不能为空");
                return false;

            } else if (fileName == "" || fileName == null) {
                alert("请选择文件");
                return false;
            } else {
                var formData = new FormData(); //必须得用formData
                formData.append("name", name);
                formData.append("fileName", fileName);
                $.ajax({
                    type: "post",
                    data: formData,//必须得用这个格式，不然报错
                    url: "/add",
                    processData: false,  //// 使数据不做处理
                    contentType: false  // 不要设置Content-Type请求头
                    , success: function (data) {
                        console.log(data);
                        if (data.msg == "ok") {
                            layer.msg("注册成功", {time: 3000}, function () {
                                location.href = "/tologin";//去登录页
                            });


                        } else {
                            layer.msg(data.msg, {time: 3000}, function () {
                                location.href = "/toadd";//去登录页
                            })


                        }
                    }
                })

                console.log("点击")

            }
        })
    });
</script>
</body>
</html>